/**
 * 
 */
package com.demo2do.boyle.backend.web.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.system.VariableService;
import com.demo2do.boyle.common.entity.system.Variable;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;

/**
 * @author Leo
 *
 */
@Controller
@RequestMapping("/variables")
@MenuSetting("menu-system")
public class VariableController {
	
	@Autowired
	private VariableService variableService;
	
	/**
	 * list all variables
	 * 
	 */
	@RequestMapping("")
    @MenuSetting("submenu-system-variable-list")
    public ModelAndView list() {
    	
        ModelAndView modelAndView = new ModelAndView("system/variable/variable-list");
        
        List<Variable> variables = variableService.list();
        modelAndView.addObject("variables", variables);
        
        return modelAndView;
    }
	
	/**
     * display the edit panel
     * 
     * @param id
     * @return the variable info edit view
     */
    @RequestMapping("/{id}/edit")
    public ModelAndView edit(@PathVariable("id") Integer id) {
    	
        ModelAndView modelAndView = new ModelAndView("system/variable/variable-edit");
        
        Variable variable = variableService.load(Variable.class, id);
        
        modelAndView.addObject("variable", variable);
        
        return modelAndView;
    }
    
    /**
     * do edit 
     * 
     * @param variable
     * @return
     */
    @RequestMapping(value="/{id}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading(value="id", type = Integer.class) Variable variable) {

        Result result = new Result();

        try {

        	variableService.update(variable);
            result.success().message("系统参数更新成功！");

        } catch(Exception e) {
            result.fail().message("服务器异常！");
        }

        return JsonUtils.toJsonString(result);	
    }   
    
    /**
     * display the create panel
     * 
     * @return
     */
    @RequestMapping("/create")
    public String create() {
        return "system/variable/variable-create";
    }
    
    /**
     * do create
     * 
     * @param variable
     * @return the result
     */
    @RequestMapping( value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(Variable variable) {
    	
    	Result result = new Result();
    		
    	variableService.save(variable);
    		
    	result.success().message("创建系统参数成功！");
    	
    	return JsonUtils.toJsonString(result);    	
    
    }
    
}
